import React, { Component } from 'react';
import { Modal, Button, Input, Row, Col, Form, message, Upload } from 'antd';
import BaseComponent from '@/components/BaseComponent';
import { connect } from 'dva';
// import { saveAs } from 'file-saver';

@connect(({ user }) => ({
  // data: risk.riskData,
  currentUser: user.currentUser,
}))
export default class index extends BaseComponent {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      uploadLoading: false,
    };
  }

  showModal = () => {
    this.setState({
      visible: true,
    });
  };

  handleOk = () => {
    this.setState({
      visible: false,
    });
  };

  handleCancel = e => {
    this.setState({
      visible: false,
    });
  };

  handleUploadBefore = (file, fileList) => {
    this.setState({ uploadLoading: true }, () => {
      this.hide = this.loadingMessage('正在上传...');
    });
    return true;
  };

  render() {
    const uploadProps = {
      name: 'file',
      action: `/api/cdcerserver/v1/followupList/importGJData?user=${this.props.currentUser.id}`,
      accept: '.xlsx,.xls',
      headers: {
        authorization: 'authorization-text',
      },
      onChange: info => {
        console.log(info);
        if (info.file.status !== 'uploading') {
          this.setState({
            fileList: info.fileList,
          });
        }
        if (info.file.status === 'done') {
          if (this.hide) {
            this.hide();
          }
          this.setState({
            uploadLoading: false,
          });
          // this.successMessage('上传成功,请等待后台处理');
          this.successMessage(info.file.response.message);
          if (info.file.response.data != null) {
            const str = info.file.response.data.errorFile;
            let a = document.createElement('a');
            a.href = str;
            a.click();
          }
          this.props.getDataByPage({});
          this.handleOk();
        } else if (info.file.status === 'error') {
          this.errorMessage('文件上传出错,请联系管理员');
        }
      },
    };
    return (
      <div style={{ display: 'inline-block' }}>
        <Button type="primary" style={{ marginLeft: '10px' }} onClick={this.showModal}>
          国家数据导入
        </Button>

        <Modal
          title="导入数据下载"
          visible={this.state.visible}
          onOk={this.handleOk}
          onCancel={this.handleCancel}
        >
          <a href="https://wny-upload2.oss-cn-hangzhou.aliyuncs.com/nongai/countryImport/%E9%9A%8F%E8%AE%BF%E5%88%97%E8%A1%A8.xls">
            下载导入表
          </a>
          <br />
          <Upload
            {...uploadProps}
            beforeUpload={this.handleUploadBefore}
            showUploadList={false}
            disabled={this.state.uploadLoading}
          >
            <Button type="primary" loading={this.state.uploadLoading}>
              数据导入
            </Button>
          </Upload>
        </Modal>
      </div>
    );
  }
}
